import time
from two_test import fontcolor_change as font

lendinformation = []  #  借书的学生信息
num = []

class Booknumber(object):

    def __init__(self,borrowed=0, borrow=0):
        """
        :param borrowed: 已借出书籍数
        :param borrow: 库存数量
        """
        self.borrowed = borrowed
        self.borrow = borrow

class Num():

    dict_num = False

    def number(self):
        book1 = Booknumber(0, 5)
        book11 = '借出{},库存{}'.format(book1.borrowed, book1.borrow)
        num.append(book11)

        book2 = Booknumber(1, 9)
        book22 = '借出{},库存{}'.format(book2.borrowed, book2.borrow)
        num.append(book22)

        book3 = Booknumber(0, 10)
        book33 = '借出{},库存{}'.format(book3.borrowed, book3.borrow)
        num.append(book33)

        book4 = Booknumber(5, 5)
        book44 = '借出{},库存{}'.format(book4.borrowed, book4.borrow)
        num.append(book44)

        book_number = dict(zip(Bookmanage.bookname,num))
        Bookmanage.books_number = book_number
        print(font.Ok + "初始化书籍信息成功" + font.END)
        book_number = dict(zip(Bookmanage.bookname, num))
        Num.dict_num = book_number
# book1 = Booknumber(0,5)
# print(book1.book)

class Bookmanage():
    bookname = ['浮生六记', '活着', '雪落香杉树', '人间失格']
    # num = []
    books_dict = False

    def information(self):
        bookinformation = ['是部自传体散文,描述了作者和妻子陈芸情投意合，想要过一种布衣蔬食而从事艺术的生活，由于封建礼教的压迫与贫困生活的煎熬，终至理想破灭,是一部水平极高影响颇大的自传体随笔，在清代笔记体文学中占有相当重要的位置。该书的特点在于真纯率真，独抒性灵，不拘格套，富有创造性。']
        bookinformation.append('作者余华,阐述了活着究竟是什么.读了此书,通过余华明白了最简单的道理:除了生死,别无大事')
        bookinformation.append('以谋杀案的提审过程为主线,交织展现了种族歧视,情感与道德选择,爱与宽恕等主题,细腻而深刻')
        bookinformation.append('又名为丧失为人的资格')
        books = dict(zip(Bookmanage.bookname,bookinformation))
        Bookmanage.books_dict = books

    # def number(self):
    #     book1 = Booknumber(0,5)
    #     book11 = '借出{},库存{}'.format(book1.borrowed,book1.borrow)
    #     Bookmanage.num.append(book11)
    #
    #     book2 = Booknumber(1,9)
    #     book22 = '借出{},库存{}'.format(book2.borrowed, book2.borrow)
    #     Bookmanage.num.append(book22)
    #
    #     book3 = Booknumber(0,10)
    #     book33 = '借出{},库存{}'.format(book3.borrowed, book3.borrow)
    #     Bookmanage.num.append(book33)
    #
    #     book4 = Booknumber(5,5)
    #     book44 = '借出{},库存{}'.format(book4.borrowed, book4.borrow)
    #     Bookmanage.num.append(book44)
    #
    #     book_number = dict(zip(Bookmanage.bookname,Bookmanage.num))
    #     Bookmanage.books_number = book_number
    #     print(font.Ok + "初始化书籍信息成功" + font.END)

# print(Bookmanage.books_number)
class Action():

    def isleapyear(self,year):
        return year%400==0 or (year % 100!=0 and year%4 ==0)

    def havedays(self,year,month):
        list31 = [1, 3, 5, 7, 8, 10, 12]
        list30 = [4, 6, 9, 11]
        if month in list31:
            return 31
        elif month in list30:
            return 30
        elif month == 2:
            if self.isleapyear(year):
                return 29
            else:
                return 28
        else:
            return 0

    def find(self, name):
        """
        根据用户传入的书籍名称判断书籍是否存在,
        如果存在, 返回该书籍对象;
        如果不存在, 返回None
        """
        for book in Bookmanage.bookname:
            if book == name:
                return book

        return  font.Error + "书籍%s不存在" %(name) + font.END

    def chat_explain(self):
        lendnumber = input('请问您要借几本书 ?')
        lendnumber = int(lendnumber)
        while lendnumber > 0:
            name = input('请把你将要借的书籍拿过来:')
            book = self.find(name)
            # booknumber = Booknumber()
            # booknumber = Bookmanage()
            if book:
                if lendnumber > 0:
                    if Booknumber.borrow > 0:
                        Booknumber.borrowed += lendnumber
                        Booknumber.borrow -= lendnumber
                        print(font.Ok + "书籍借阅成功" + font.END)
                    else:
                        print(font.warning + "书籍%s库存不足" % (name) + font.END)
                pass
            else:
                print(font.Error + "书籍%s不存在" %(name) + font.END)
            print(Bookmanage.books_dict[name])
            lendnumber = int(lendnumber)
            lendinformation.append(name)
            lendnumber -= 1
        return lendinformation

    def recordlend(self):
        stime1 = time.localtime()
        stime2 = time.strftime('%Y -%m -%d', stime1)
            # print(time.strftime('%Y', stime1))
            # print(time.strftime('%m', stime1))
            # print(time.strftime('%d', stime1))
        return '{}:借出书籍'.format(stime2)


    def recordreturn(self):
        day = input('最多借30天您借几天?')
        day = int(day)
        stime1 = time.localtime()
        stime_year = time.strftime('%Y',stime1)
        stime_year = int(stime_year)
        stime_month = time.strftime('%m',stime1)
        stime_month = int(stime_month)  # 6
        stime_day = time.strftime('%d',stime1)
        stime_day = int(stime_day)     # 25

        res = self.havedays(stime_year, stime_month)    #30
        day1 = stime_day+day
        if day1 > res:
            stime_month = stime_month+1
            stime_day = day1-res

            res = '应{}-{}返回书籍'.format(stime_month,stime_day)
            return res

class Students():
    def __init__(self,students = []):
        self.students = students

    def add(self,sid,name,age,bookname,lenddate,returndate):
        student = {'sid':sid,'name':name,'age':age,'bookname':bookname,'lenddate':lenddate,'returndate':returndate}
        self.students.append(student)


def main():
    ac = Action()
    bm = Bookmanage()
    bm.information()
    n = Num()
    print("""
    书籍借阅与管理系统
    1).查询书籍信息
    2).借书
    3).归还书籍
    4).查询学生情况
    5).退出系统
    请输入你的选择：""")
    while 1:
        print()
        a = input('请输入行为指令:')
        if a == '2':
            print(Bookmanage.bookname)
            lendname = ac.chat_explain()
            lenddate = ac.recordlend()
            returndate = ac.recordreturn()
            print('请将书交给管理员并进行信息登记')
            register_sid = input('请输入学号:')
            register_name = input('请输入姓名:')
            register_age = input('请输入年龄:')
            information = 'lendbookname:{},name:{},age:{},lenddate:{},returndate:{}'.format(lendname,register_name,register_age,lenddate,returndate)
            bm.books_dict[register_sid] = information
            print(bm.books_dict)

        elif a == '1':
            print(n.dict_num)

        elif a == '3':
            break
main()

















































